#ifndef _MEGA_GAMEAPPLICATION_H_
#define _MEGA_GAMEAPPLICATION_H_

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <vector>

#include "Globals.h"
#include "IEventsHandler.h"

namespace mega
{

// forward declarations
class GameView;

/**
 * Main class implemented as singleton.
 * Owns render window and stack of views.
 * It also provides main loop in run() method corresponding to top view in stack.
 */
class GameApplication
	: public IEventsHandler
{
public:
	~GameApplication();

private:
	GameApplication();

	GameApplication(const GameApplication & _rhs);
	GameApplication & operator=(const GameApplication & _rhs);

public:
	static GameApplication & instance();

	sf::RenderWindow & window();

	// managing views
	void pushView(GameView * _view);
	void popView();

	// main loop
	int run();

	// this method is processing all the events listed below respectively to views vector
	void processEvents();

	// overriding events handler interface
	bool onKeyPressed (const sf::Keyboard::Key & _key);
	bool onKeyReleased(const sf::Keyboard::Key & _key);
	bool onMousePressed (const sf::Mouse::Button & _button, const Position & _pos);
	bool onMouseReleased(const sf::Mouse::Button & _button, const Position & _pos);

private:
	static const char * m_windowTitle;

private:
	typedef std::vector<GameView*> GameViewCollection;

	sf::RenderWindow   m_window;
	GameViewCollection m_views;
	size_t             m_currentViewIndex;
};

extern GameApplication & gApp();

}

#endif // _MEGA_GAMEAPPLICATION_H_
